package com.oa.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.joneying.common.annotation.CommonController;
import com.joneying.common.web.response.Resp;
import com.oa.admin.entity.UserHoliday;
import com.oa.admin.service.IUserHolidayService;
import com.oa.core.BaseController;
import com.oa.system.entity.ApplyApproval;
import com.oa.system.entity.User;
import com.oa.system.service.IApplyApprovalCopyRecordService;
import com.oa.system.service.IApplyApprovalService;
import com.oa.system.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("主页信息")
@RestController
@RequestMapping("/index")
public class IndexController extends BaseController {

    @Autowired
    private IApplyApprovalService applyApprovalService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserHolidayService userHolidayService;

    @Autowired
    private IApplyApprovalCopyRecordService applyApprovalCopyRecordService;

    @GetMapping("/date")
    @CommonController
    @Description("服务器时间")
    @ApiOperation(value = "服务器时间")
    public Resp getDate(){
        return new Resp().success(new Date());
    }

    @GetMapping("/statistical")
    @CommonController
    @Description("查询统计数")
    @ApiOperation(value = "查询统计数")
    public Resp statisticalNumber(){
        Integer initiateApproval = applyApprovalService.selectCount(
                new EntityWrapper<ApplyApproval>()
                        .eq("approval_user_id", getLoginUserId())
                        .ne("approval_node_id", "0")
        );

        Integer mailList = userService.selectCount(new EntityWrapper<User>().eq("is_deleted", 2));

        List<UserHoliday> userHolidays = userHolidayService.selectList(new EntityWrapper().eq("user_id", getLoginUserId()));
        Double vacationBalance = 0.0;

        for(int i = 0; i < userHolidays.size(); i++){
            vacationBalance += userHolidays.get(i).getSurplusDay();
        }


        Map<String, Object> data = new HashMap<>();
        data.put("initiateApproval", initiateApproval);
        data.put("remind", 0);
        data.put("mailList", mailList);
        data.put("vacationBalance", vacationBalance);
        return new Resp().success(data);
    }

    @GetMapping("/mobile/statistical")
    @CommonController
    @Description("查询统计数")
    @ApiOperation(value = "查询统计数")
    public Resp mStatisticalNumber(){
        Integer iApprove = applyApprovalService.selectCount(new EntityWrapper().where("approval_node_id in (select id from sys_approval_node_user where user_id = concat('%', {0}, '%'))", getLoginUserId()));
        Integer iInitiated = applyApprovalService.selectCount(new EntityWrapper().eq("approval_user_id", getLoginUserId()));
        Integer copyMe = applyApprovalCopyRecordService.selectCount(new EntityWrapper().eq("user_id", getLoginUserId()));
        Map<String, Object> data = new HashMap<>();
        data.put("iApprove", iApprove);
        data.put("iInitiated", iInitiated);
        data.put("copyMe", copyMe);

        return new Resp().success(data);
    }
}
